Encyclopedia [B]
Memory limit: 64 MB
Little John loves reading Bytean encyclopedia.
He is especially fascinated with the colorful illustrations the book contains.
Bytean encyclopedia consists of many independent parts.
Once in a while some new pages are printed.
John's parents then add them to a binder containing all previous pages of the encyclopedia.
In order to protect encyclopedia's pages from getting dirty, John's parents put each of them into a separate transparent shirt.
One day John dropped the book on the floor - all shirts fell out of the binder and all pages fell out of the shirts.
Luckily, nothing got lost (neither pages nor shirts) and the number of pages is still equal to the number of shirts.
John picked up all elements from the floor and put all of them together, forming a stack. He wants to put all elements back into the
binder. Firstly, he needs to reorder pages and shirts in the stack so that pages are interleaved by shirts.
John can't read, so the order of pages is not an issue for him. The only important thing is that all pages should be located back in shirts.
In each move John can swap positions of two consecutive elements in the stack.
He finishes the process of reording when pages and shirts occur in the stack alternately.
Help Little John and calculate how many swap operations of consecutive elements in the stack are necessary to perform the desired reordering.
Task
Write a program which:
- reads from the standard input the description of the stack,
- calculates how many swap operations are required to order elements of Bytean encyclopedia,
- writes the result to the standard output.
Input
The first line of the standard input contains one integer ( representing the number of pages
(which is also equal to the number of shirts) in the Bytean encyclopedia.
The following lines contain the description of the stack: non-negative integers.
The -th number describes -th element on the stack and is equal , if that element is a shirt.
Otherwise it is a positive number not grater than .
Description of the stack contains the same number of zeros as positive numbers.
Encyclopedia is not perfect and pages numbers might repeat.
Output
One integer should be written to the standard output -
the minimal number of swap operations that have to be performed to put Bytean encyclopedia back together.
Example
For the input data:
5
5
1
0
0
2
4
0
3
0
0
the correct result is:
3
Task author: Krzysztof Duleba.